/*
 * Copyright (c) 2010, Michael Hengler (developer (at) hengler (dot) de),
 * Nikolaus Moll (developer (at) miblounge (dot) net).
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 * * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * * Neither the name of the Jombi project nor the
 * names of its contributors may be used to endorse or promote products
 * derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL JOMBI BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 */
package net.miblounge.jombi.impl.widgets;

import net.miblounge.jombi.impl.options.QtOptions;
import net.miblounge.jombi.impl.options.SplitlayoutMode;
import net.miblounge.jombi.impl.util.BorderConvert;
import net.miblounge.jombi.impl.util.DimensionConvert;
import net.miblounge.jombi.impl.util.SplitOrientationConvert;
import net.miblounge.jombi.impl.widgets.base.JoSplitPane;

import org.jowidgets.common.types.Dimension;
import org.jowidgets.common.types.SplitResizePolicy;
import org.jowidgets.common.widgets.factory.IGenericWidgetFactory;
import org.jowidgets.spi.widgets.ICompositeSpi;
import org.jowidgets.spi.widgets.ISplitCompositeSpi;
import org.jowidgets.spi.widgets.setup.ISplitCompositeSetupSpi;

import com.trolltech.qt.gui.QFrame;
import com.trolltech.qt.gui.QSplitter;
import com.trolltech.qt.gui.QWidget;

public class SplitCompositeImpl extends QtControl implements ISplitCompositeSpi {

	private final ICompositeSpi first;
	private final ICompositeSpi second;

	private final QFrame firstFrame;
	private final QFrame secondFrame;

	public SplitCompositeImpl(
		final Object parentUiReference,
		final IGenericWidgetFactory factory,
		final ISplitCompositeSetupSpi setup) {
		super(new JoSplitPane(SplitOrientationConvert.convert(setup.getOrientation()), setup.getWeight(), getResizeWeight(setup)));
		getUiReference().setParent((QWidget) parentUiReference);

		getUiReference().setOpaqueResize(QtOptions.getSplitLayoutMode() == SplitlayoutMode.ON_MOUSE_MOVE);
		firstFrame = new QFrame();
		secondFrame = new QFrame();

		firstFrame.setFrameStyle(BorderConvert.convert(setup.getFirstBorder()));
		secondFrame.setFrameStyle(BorderConvert.convert(setup.getSecondBorder()));

		first = new QtComposite(factory, firstFrame);
		second = new QtComposite(factory, secondFrame);

		first.setLayout(setup.getFirstLayout());
		second.setLayout(setup.getSecondLayout());

		final QSplitter splitPane = getUiReference();
		splitPane.addWidget(firstFrame);
		splitPane.addWidget(secondFrame);
	}

	@Override
	public ICompositeSpi getFirst() {
		return first;
	}

	@Override
	public ICompositeSpi getSecond() {
		return second;
	}

	@Override
	public QSplitter getUiReference() {
		return (QSplitter) super.getUiReference();
	}

	private static double getResizeWeight(final ISplitCompositeSetupSpi setup) {
		final SplitResizePolicy resizePolicy = setup.getResizePolicy();
		if (resizePolicy == SplitResizePolicy.RESIZE_FIRST) {
			return 1;
		} else if (resizePolicy == SplitResizePolicy.RESIZE_SECOND) {
			return 0;
		} else if (resizePolicy == SplitResizePolicy.RESIZE_BOTH) {
			return setup.getWeight();
		} else {
			throw new IllegalArgumentException(SplitResizePolicy.class.getSimpleName() + "'" + resizePolicy + "' is not known.");
		}
	}

	@Override
	public void setMinSizes(final Dimension firstMinSize, final Dimension secondMinSize) {
		firstFrame.setMinimumSize(DimensionConvert.convert(firstMinSize));
		secondFrame.setMinimumSize(DimensionConvert.convert(secondMinSize));
	}
}
